package com.aistar.service.impl;

import com.aistar.mapper.AddressMapper;
import com.aistar.pojo.Address;
import com.aistar.pojo.AddressExample;
import com.aistar.service.AddressService;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    //通过用户id 查询该用户地址的个数
    @Override
    public ServerResponse getAddressCount(Integer customerId) {
       AddressExample addressExample =  new AddressExample();
        System.out.println("customerId:"+customerId);
       addressExample.createCriteria().andAddressStatusEqualTo(1).andCustomerIdEqualTo(customerId);
       //一个用户可能拥有多个地址 list  多个地址信息
       List<Address> addressList =  addressMapper.selectByExample(addressExample);

       Integer count = addressList.size();

        System.out.println("count:"+count);
       /*if (addressList !=null && addressList.size()>0){
           return ServerResponse.getDataSuccess(count);
       }else {
           return ServerResponse.getDataFailed("您目前还没有地址");
       }*/
        return ServerResponse.getDataSuccess(count);
    }


    //不分页查询所有地址 用户的
    @Override
    public ServerResponse getAllByCustomerId(Integer customerId) {
        AddressExample addressExample = new AddressExample();
        addressExample.createCriteria().andAddressStatusEqualTo(1)
                .andCustomerIdEqualTo(customerId);
        List<Address> addressList = addressMapper.selectByExample(addressExample);
        if (addressList != null && addressList.size()>0){
            return ServerResponse.getDataSuccess(addressList);
        }else {
            return ServerResponse.getDataFailed("暂无地址");
        }

    }

    //分页查询所有地址 用户的
    @Override
    public ServerResponse getAllPageCustomerId(Integer customerId, Integer pageNum) {
        AddressExample addressExample = new AddressExample();
        addressExample.createCriteria().andAddressStatusEqualTo(1)
                .andCustomerIdEqualTo(customerId);
        Page<Address> page = PageHelper.startPage(pageNum,5);
        List<Address> addressList = addressMapper.selectByExample(addressExample);
        PageInfo<Address> pageInfo = page.toPageInfo();
        if (addressList != null && addressList.size()>0){
            return ServerResponse.getDataSuccess(pageInfo);
        }else {
            return ServerResponse.getDataFailed("您暂未无收获地址");
        }


    }

    //添加一条地址
    @Override
    public ServerResponse save(Address address) {
        int rows = addressMapper.insert(address);
        if (rows>0){
            return ServerResponse.addSuccess();
        }else {
            return ServerResponse.addFailed();
        }
    }

    //此方法是删除方法,将地址的状态修改为0
    @Override
    public ServerResponse modifyStatus(Address address) {
        address.setAddressStatus(0);
        int rows = addressMapper.updateByPrimaryKeySelective(address);
        if (rows>0){
            return ServerResponse.deleteSuccess();
        }else {
            return ServerResponse.deleteFailed();
        }

    }

    //这是修改
    @Override
    public ServerResponse modify(Address address) {
        int rows = addressMapper.updateByPrimaryKey(address);
        if (rows>0){
            return ServerResponse.updateSuccess();
        }else {
            return ServerResponse.updateFailed();
        }
    }

    @Override
    public ServerResponse getById(Integer id) {
        AddressExample addressExample = new AddressExample();
        addressExample.createCriteria().andAddressStatusEqualTo(1)
                                        .andAddressIdEqualTo(id);
        List<Address> addressList =  addressMapper.selectByExample(addressExample);
        if (addressList != null && addressList.size()>0){
            return ServerResponse.getDataSuccess(addressList);
        }else {
            return ServerResponse.getDataFailed("查询失败，并无此地址");
        }
    }


    @Override
    public ServerResponse getAll() {

        return null;
    }
}
